package com.zdw.oauth.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.zdw.common.parent.JSONResult;
import com.zdw.oauth.config.international.InternationalUtils;
import com.zdw.oauth.dto.UserSelectDto;
import com.zdw.oauth.entity.UserEntity;
import com.zdw.oauth.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 卓德文
 * @title: 登录控制器
 * @date 2022/12/22
 */
@Api(tags = "用户认证相关")
@Slf4j
@RestController
@RequestMapping("/sa-token")
public class SaTokenController {

    @Resource
    private UserService userService;

    /**
     * 手机登录
     *
     * @return
     */
    @ApiOperation(value = "手机登录")
    @PostMapping("/phoneLogin")
    public JSONResult phoneLogin(@RequestBody UserSelectDto userSelectDto) {
        JSONResult jsonResult = new JSONResult();
        //查询并匹对账号密码
        UserEntity userEntity = userService.selectByPhone(userSelectDto.getPhone());
        if (userSelectDto.getPhone().equals(userEntity.getPhone()) && userSelectDto.getPassword().equals(userEntity.getPassword())) {
            StpUtil.login(userEntity.getId());
            return jsonResult.markSuccess(InternationalUtils.getInternational("oauth.login.success"), StpUtil.getTokenInfo());
        }
        return jsonResult.markFail(InternationalUtils.getInternational("oauth.login.fail"));
    }

    /**
     * 账号登录
     *
     * @return
     */
    @ApiOperation(value = "账号登录")
    @PostMapping("doLogin")
    public JSONResult doLogin(@RequestBody UserSelectDto userSelectDto) {
        JSONResult jsonResult = new JSONResult();
        //查询并匹对账号密码
        UserEntity userEntity = userService.selectByAccount(userSelectDto.getAccount());
        if (userSelectDto.getAccount().equals(userEntity.getAccount()) && userSelectDto.getPassword().equals(userEntity.getPassword())) {
            StpUtil.login(userEntity.getId());
            return jsonResult.markSuccess(InternationalUtils.getInternational("oauth.login.success"), StpUtil.getTokenInfo());
        }
        return jsonResult.markFail(InternationalUtils.getInternational("oauth.login.fail"));
    }

    /**
     * 查询登录状态
     *
     * @return
     */
    @ApiOperation(value = "查询登录状态")
    @PostMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok(InternationalUtils.getInternational("oauth.login.isLogining") + StpUtil.isLogin());
    }

    /**
     * 查询Token信息
     *
     * @return
     */
    @ApiOperation(value = "查询Token信息")
    @PostMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(InternationalUtils.getInternational("oauth.login.tokenInfo") + StpUtil.getTokenInfo());
    }

    /**
     * 注销
     *
     * @return
     */
    @ApiOperation(value = "注销")
    @GetMapping("logout")
    public SaResult logout(Integer loginId) {
        StpUtil.logout(loginId);
        return SaResult.ok(InternationalUtils.getInternational("oauth.login.logout") + loginId);
    }

}